<?php
require_once('admin'.EXT);

class Module extends Admin{

	function __construct()
	{
		parent::__construct();
		$this->load->model(array('module_model','permission_model'));
		$this->data['rows_module']	=	$this->module_model->get_by_parent_id();
	}
	
	function index()
	{
		$this->data['rows_par']		=	$this->module_model->get_by_parent_id(0);
		$this->data['tpl_file']		=	'module/index';
		$this->load->view('layout/default',$this->data);
	}
	
	function add()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$parent_id	=	$this->input->post('parent_id');
			$title		=	$this->input->post('title');
			$name		=	$this->input->post('name');
			$css_class	=	$this->input->post('class');
			$this->form_validation->set_rules('name','Name','required');
			$this->form_validation->set_rules('title','Title','required');
			
			if($this->form_validation->run()===TRUE)
			{
				$data = array(
					'parent_id'	=>	$parent_id,
					'title'		=>	$title,
					'name'		=>	$name,
					'css_class'	=>	$css_class
				);
				$this->module_model->insert($data);
			}
			else
			{
				$this->message->error	=	$this->form_validation->get_error_array();
			}
		}
		$this->data['tpl_file']		=	'module/add';
		$this->load->view('layout/default',$this->data);
	}
	
	function edit()
	{
		if(!isset($_GET['id']) || $_GET['id'] == '') redirect(admin_url('module'));
		$this->data['row_module']	=	$this->module_model->get_by(array('id' => $_GET['id']));
		if($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			$parent_id	=	$this->input->post('parent_id');
			$title		=	$this->input->post('title');
			$name		=	$this->input->post('name');
			$order		=	$this->input->post('order');
			$css_class	=	$this->input->post('class');
			$this->form_validation->set_rules('name','Name','required');
			$this->form_validation->set_rules('title','Title','required');
			
			if($this->form_validation->run()===TRUE)
			{
				$data = array(
					'parent_id'	=>	$parent_id,
					'title'		=>	$title,
					'name'		=>	$name,
					'css_class'	=>	$css_class,
					'order'		=>	$order
				);
				$this->module_model->update($_GET['id'],$data);
				redirect(admin_url('module'));
			}
			else
			{
				$this->message->error	=	$this->form_validation->get_error_array();
			}
		}
		$this->data['tpl_file']		=	'module/edit';
		$this->load->view('layout/default',$this->data);
	}
	
	function assign()
	{
		$this->load->model(array('group_model','permission_model'));
		if(isset($_GET['gid']))
		{
			$gid	=	$_GET['gid'];
			$this->data['rows_module_none']		=	$this->module_model->get_trees(0,'---');
			$this->data['rows_module']			=	$this->module_model->get_by_group_id($gid);
			
			if(isset($_GET['id']) && $_GET['id'] != '')
			{
				$this->data['row_module']	=	$this->module_model->get_by_id($_GET['id']);
			}
			
			if($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$mid	=	$this->input->post('module');
				$method =	$this->input->post('method');
				$status	=	$this->input->post('status');
				$status =	($status == 'active' ? 'active' : 'inactive');
				$this->form_validation->set_rules('method','Method','required');
				if($this->form_validation->run()===true)
				{
					
					if(isset($_GET['id']) && $_GET['id'] != '')
					{
						$data = array(
							'group_id'		=>	$gid,
							'module_id'		=>	$mid,
							'method'		=>	$method,
							'status'		=>	$status
						);
						$this->permission_model->update($_GET['id'],$data);
						//redirect(admin_url('module/assign?gid='.$gid));
					}
					else 
					{
						$data = array(
							'group_id'		=>	$gid,
							'module_id'		=>	$mid,
							'method'		=>	$method,
							'status'		=>	$status,
							'created_on'	=>	date('Y-m-d H:i:s')
						);
						$this->permission_model->insert($data);
						//
					}
					redirect(admin_url('module/assign?gid='.$gid));
				}
				else
				{
					$this->message->error	=	$this->form_validation->get_error_array();
				}
			}
			
		}	
		$this->data['rows_group']	=	$this->group_model->get_all();
		$this->data['tpl_file']		=	'module/assign';
		$this->load->view('layout/default',$this->data);
	}
	
	function delete()
	{
		if(isset($_GET['id']) && $_GET['id'] != '')
		{
			$this->permission_model->delete($_GET['id']);
			redirect(admin_url('module/assign?gid='.$_GET['gid']));
		}
	}
}